//
//  XMEmoticonPageViewCell.swift
//  XMWeimo
//
//  Created by ios001 on 16/7/29.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class XMEmoticonPageViewCell: UICollectionViewCell {
    
    // 保存20个按钮数组
//    var buttons:[UIButton] = [UIButton]()
    var buttons:[XMEmoticonButton] = [XMEmoticonButton]()
    
    // 定义一个装有模型的数组
    var emoticons: [XMEmoticon]?{
        didSet{
            // 隐藏所有的按钮
            for value in buttons {
                value.hidden = true
            }
            
            // 遍历表情数组
            for (i,emoticon) in emoticons!.enumerate() {
                // 得到对应的button
                let button = buttons[i]
                // 赋值
                button.emoticon = emoticon
                // 显示按钮
                button.hidden = false
                // 代表是emoji表情
                if emoticon.isEmoji {
                    button.setTitle((emoticon.code ?? "").emoji(), forState: UIControlState.Normal)
                    // 解决image和title 重复的问题
                    button.setImage(nil, forState: UIControlState.Normal)
                }else {
                    // 代表图片表情
                    let image = UIImage.getEmoticonImage(emoticon.path)
                    button.setImage(image, forState: UIControlState.Normal)
                    // 解决image和title 重复的问题
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
            }
        }
    }
    
    var indexPath: NSIndexPath?{
        didSet{
            messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 监听事件
    // 删除按钮点击发送通知
    func deleteButtonClick(){
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonDeleteButtonNofi, object: nil)
    }
    
    // 表情按钮点击发送通知
    func buttonClick(button: XMEmoticonButton) {
        NSNotificationCenter.defaultCenter().postNotificationName(WBEmoticonButtonNoti, object: button.emoticon)
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        addChildButtons()
        // 添加控件
        contentView.addSubview(deleButton)
        // 添加约束
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 按钮的宽高
        let childW = (self.frame.width - 10) / CGFloat(XMEmoticonMaxCol)
        let childH = (self.frame.height - 10) / CGFloat(XMEmoticonMaxRow)
        // 遍历按钮数组,给button设置frame
        for (i,value) in buttons.enumerate() {
            let col = CGFloat(i%XMEmoticonMaxCol)
            let row = CGFloat(i/XMEmoticonMaxCol)
            value.frame = CGRect(x: childW*col + 5, y: childH*row, width: childW, height: childH)
        }
        // 删除按钮的frame
        deleButton.frame = CGRect(x: self.frame.width - childW - 5, y: childH*2, width: childW, height: childH)
    }
    
    // 添加按钮
    func addChildButtons(){
        for _ in 0..<XMEmoticonMaxCount {
            let button = XMEmoticonButton()
            // 添加点击事件
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
            contentView.addSubview(button)
            buttons.append(button)
        }
    }
    
    // MARK: - 懒加载控件
    private lazy var messageLabel: UILabel = {
       let lab = UILabel(textColor: UIColor.redColor(), textFont: 35, maxWidth: SCREENW)
        lab.textAlignment = .Center
        return lab
    }()
    
    // 删除按钮
    private lazy var deleButton: UIButton = {
       let button = UIButton()
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        // 添加事件
        button.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()

}
